#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;

const int N = 4e5 + 10;

int h[N], e[N], ne[N], fa[N], idx;
int n, k;
bool st[N];
int maxh;
int cnt;

void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

int dfs(int u){
    int maxv = 0;

    for(int i = h[u]; ~i; i = ne[i]){
        int j = e[i];
        maxv = max(maxv, dfs(j));
    }

    maxv ++;

    if(maxv == maxh && fa[u] != 1){
    //    cout << "u: " << u << endl;
        cnt ++;
        return 0;
    }

    return maxv;


}

bool check(int h){
    maxh = h;
    cnt = 0;
    dfs(1);

    if(cnt > k) return false;
    return true; 
}


void solve(){
    cin >> n >> k;
    for(int i = 0; i <= n; i ++) h[i] = -1, st[i] = false;
    idx = 0;
    fa[1] = 1;
    int p;
    for(int i = 2; i <= n; i ++){
        cin >> p;
        add(p, i);
        fa[i] = p;
    }

    int l = 1, r = n - 1;
    // check(1);
    while(l < r){
        int mid = l + r >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }

    cout << l << '\n';


}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}